//
//  RequestDatas.m
//  shuai
//
//  Created by xingshuai on 2019/7/13.
//  Copyright © 2019 xingshuai. All rights reserved.
//

#import "RequestDatas.h"

static RequestDatas *requests = nil;

@implementation RequestDatas

/** 单例实现 */
+(instancetype)shareRequestDatas
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        requests = [[RequestDatas alloc]init];
    });
    return requests;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        requests = [super allocWithZone:zone];
    });
    return requests;
}


/** 网络请求 */
+(void)requestWithMethod:(RequestMethodType)methodType withUrl:(NSString *)url withIsPullDown:(BOOL)isPullDown withParameter:(NSDictionary *)parameter success:(void(^)(id response))success failure:(void(^)(NSError *err))failure
{
    kWeakSelf(self);
    AFHTTPSessionManager *mgr = [weakself Manger];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    switch (methodType){
            
        case RequestMethodTypeGet:
        {
            [mgr GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
                //这里可以获取到目前数据请求的进度
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                if(success)
                {
                    success(responseObject);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                if(failure)
                {
                    failure(error);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            }];
            break;
        }
        
        case RequestMethodTypePost:
        {
            [mgr POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
                //这里可以获取到目前数据请求的进度
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                if(success)
                {
                    success(responseObject);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                if(failure)
                {
                    failure(error);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            }];
            break;
        }
            
//        case RequestMethodTypeCustomPost:
//        {
//            break;
//        }
//
//        case RequestMethodTypeLoginOrRegister:
//        {
//            break;
//        }
            
        case RequestMethodTypeImage:
        {
            [mgr POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                
                NSData *data = parameter[@""];
                
                if (!data) {
                    data = [NSData data];
                }
                [formData appendPartWithFileData:data name:@"headPortrait" fileName:@"headPortrait.jpg" mimeType:@"image/jpeg"];
                
            } progress:^(NSProgress * _Nonnull uploadProgress) {
                // 进度
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                if(success)
                {
                    success(responseObject);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                if(failure)
                {
                    failure(error);
                }
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                
            }];
                        
            break;
        }
            
        default:
            break;
    }
}

+(AFHTTPSessionManager *)Manger
{
    NSURL *myBaseUrl = [NSURL URLWithString:shuai_baseUATURL];
    /** 创建请求管理管理对象 */
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:myBaseUrl];
    /** 过滤nsnull参数 */
    ((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
    /** 请求数据和接收数据的格式 */
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer  = [AFJSONRequestSerializer  serializer];
    //[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content－Type"];
    /** 设置请求成功后的接收内容类型 */
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"text/plain",@"application/json", @"image/jpeg",@"image/png", nil];
    
    return manager;
}

/** 检测网络连接状况 */
+ (void)netWorkMonjor
{
    /** 创建网络状态检测管理对象 */
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    /** 开启监听，否则不走block */
    [manager startMonitoring];
    /** 监听改变 */
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status)
        {
                case AFNetworkReachabilityStatusUnknown:
                //[SVProgressHUD showErrorWithStatus:@"未知网络"];
                NSLog(@"111");
                break;
                case AFNetworkReachabilityStatusNotReachable:
                //[SVProgressHUD showErrorWithStatus:@"网络已断开"];
                NSLog(@"222");
                break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                //[SVProgressHUD showSuccessWithStatus:@"3G/4G网络已连接"];
                NSLog(@"333");
                break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                //[SVProgressHUD showSuccessWithStatus:@"WIFI已连接"];
                NSLog(@"444");
                break;
                
            default:
                break;
        }
    }];
}


@end
